import  { AppPostT } from '@/utils/request'
import { LivePageSearch, LivePageResult, Live, RecLiveSearch, RecLiveResult, RecLive } from '@/models/live'

// 分页获取直播列表
export async function getLives(data: LivePageSearch) {
    const res = await AppPostT<string>("/live/all", data)
    console.log("getLives request:", {}, ", result:", res)
    
    let result = {} as LivePageResult
    if (res.data) {
        const dataObject = JSON.parse(res.data);
        const promises = dataObject.resultData.map(async (item: any) => {
            const live = {} as Live;
            live.roomId = item.roomId;
            live.roomUrl = await getRoomUrl(item);
            live.title = item.title;
            live.imgPc = dataObject.preUrl + item.imgPc;
            live.startTime = item.startTime;
            live.endTime = item.endTime;
            live.status = item.status;
            return live;
        });

        result.records = await Promise.all(promises);
        console.log("getLives result.records:", result.records);
        result.pageNum = data.pageNum || 0;
        result.pageSize = data.pageSize || 16;
        result.count = result.records.length;
    }
    return result;
}

// 获得推荐直播列表
export async function getRecLives(data: RecLiveSearch) {
    const res = await AppPostT<string>("/live/rec", data)
    console.log("getRecLives request:", {}, ", result:", res)
    
    let result = {} as RecLiveResult
    if (res.data) {
        const dataObject = JSON.parse(res.data);

        result.currentTime = dataObject.currentTime
        result.preUrl = dataObject.preUrl
        result.code = dataObject.code
        result.message = dataObject.message

        const promises = dataObject.resultData.map(async (item: any) => {
            let recLive = {} as RecLive
            recLive.roomId = item.roomId
            recLive.roomUrl = await getRoomUrl(item)
            recLive.title = item.title
            recLive.imgPc = result.preUrl + item.imgPc
            recLive.expertName = item.expertName
            recLive.expertPic = result.preUrl + item.expertInfo.headImg
            recLive.startTime = item.startTime
            recLive.endTime = item.endTime
            recLive.status = item.status
            return recLive;
        });
        
        result.resultData = await Promise.all(promises);
        console.log("getRecLives result.resultData:", result.resultData);
    }
    return result;
}

/**
 * 获取直播/回放地址（playBackFlag：回放视频标识，0-没有，1-课程，2-百家云）
 * 1、直播地址：
 *      https://www.fangzhenxiu.com/usercenter/live/1415305660469018624
 *      1415305660469018624：对应 pkId
 * 2、回放地址：
 *      https://www.fangzhenxiu.com/course/13432581/13432910/
 *      13432581：对应courseId
 *      13432910：对应 courseVideoId
 */
async function getRoomUrl(item: object) {
    if (item == null || item.status==0) {
        return "";
    }
    if (item.status==1) {
        return `https://www.fangzhenxiu.com/usercenter/live/${item.pkId}`;
    } else if (item.status==2) {
        if (item.playBackFlag==1) {
            return `https://www.fangzhenxiu.com/course/${item.courseId}/${item.courseVideoId}/`;
        } else if (item.playBackFlag==2) {
            const data = {
                pkId: item.pkId,
            }
            const res = await AppPostT<string>("/live/getBackPlayUrlForSupercomputer", data)
            console.log("getBackPlayUrl request:", data, ", result:", res)
            
            let backPlayUrl = ""
            if (res.data) {
                const dataObject = JSON.parse(res.data); // {"currentTime":"2025-10-13 17:08:02","code":"1","resultData":"https://e62580122.at.baijiayun.com/web/playback/index?classid=25092368164061&token=a8UhDg_NWoj0FMU2HWbo30IgqcehjI8X4GkgzQa8kKGTWtA4eDrfvfzBglkIsmnovF3YnsfJFO8","preUrl":"https://public.fangzhenxiu.com/","message":"?????"}
                backPlayUrl = dataObject.resultData || "";
            }
            return backPlayUrl;
        } else {
            return "";
        }
    }
}
